package tracker;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class LogServiceListener implements ServiceTrackerCustomizer {

	// --- Members -------------------------------------------------------------
	private BundleContext bundleContext;
	private LogService logService;
	private ServiceTracker logServiceTracker;

	// --- Methods -------------------------------------------------------------

	public LogServiceListener(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
		logServiceTracker = new ServiceTracker(bundleContext,
				LogService.class.getName(), this);
		logServiceTracker.open();
	}

	public void stop() {
		logServiceTracker.close();
		logService = null;
	}

	public LogService getLog() {
		return logService;
	}

	// --- Implementation ServiceTrackerCustomizer interface -------------------
	public Object addingService(ServiceReference reference) {
		logService = (LogService) bundleContext.getService(reference);
		return logService;
	}

	public void modifiedService(ServiceReference reference, Object service) {
	}

	public void removedService(ServiceReference reference, Object service) {
		logService = null;
	}
}
